package com.mioto.ld.communication;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.communication.websocket.model.AlertNotifyBody;
import com.mioto.ld.communication.websocket.model.MessageNotifyBody;
import com.mioto.ld.communication.websocket.model.SendCmdEnum;
import com.mioto.ld.communication.websocket.model.WsSendMessage;
import com.mioto.ld.communication.websocket.util.MessageSendUtil;
import com.mioto.ld.communication.websocket.util.WsChannelUtil;
import com.mioto.ld.module.alert.model.LinkageFunctionPO;
import com.mioto.ld.module.device.model.Device;
import com.mioto.ld.module.organization.dao.OrganizationDao;
import com.mioto.ld.module.personal.model.User;
import com.mioto.ld.module.personal.service.IUserService;
import com.mioto.ld.utils.MailHelper;
import com.mioto.ld.utils.SmsHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author mioto-qinxj
 * @date 2024/5/27
 * @desc 设备通知处理类
 */
@Component
@Slf4j
public class DeviceNotifyProcess {
    private static final String NOTIFY_MODE_APP = "1";

    private static final String NOTIFY_MODE_MAIL = "2";

    private static final String NOTIFY_MODE_SMS = "3";
    @Resource
    private OrganizationDao organizationDao;
    @Resource
    private IUserService userService;
    @Resource
    private MailHelper mailHelper;
    @Resource
    private SmsHelper smsHelper;

    /**
     * 告警通知 -- websocket通知
     * @param alertId 告警配置id
     * @param device 设备
     */
    public void executeAlertNotify(Integer alertId, Device device){
        try {
            log.info("send alert notify message to app user");
            String orgName = organizationDao.findOrgNameByDeviceId(device.getId());
            String clientId = WsChannelUtil.buildAppClientId(device.getOwnerUser());
            WsSendMessage message = new WsSendMessage(SendCmdEnum.ALERT_NOTIFY);
            message.setBody(new AlertNotifyBody(device.getProductId(),orgName,alertId));
            MessageSendUtil.send(clientId,message);
        }catch (Exception e){
            log.error("send alert notify message to app user error",e);
        }
    }


    /**
     * 消息通知
     * @param linkageFunctionPO
     * @param device
     */
    public void executeMessageNotify(LinkageFunctionPO linkageFunctionPO,Device device){
        try {
            if (StrUtil.isNotEmpty(linkageFunctionPO.getNotifyModes())){
                User user = userService.findByColumn("id",device.getOwnerUser());
                log.info("execute message notify,notify modes - {}",linkageFunctionPO.getNotifyModes());
                String[] notifyModes = StrUtil.splitToArray(linkageFunctionPO.getNotifyModes(),StrUtil.C_COMMA);

                for (String notifyMode : notifyModes) {
                    switch (notifyMode){
                        case NOTIFY_MODE_APP:
                            sendApp(linkageFunctionPO,device);
                            break;
                        case NOTIFY_MODE_MAIL:
                            mailHelper.send(linkageFunctionPO.getTitle(),linkageFunctionPO.getContent(),user.getEmail());
                            break;
                        case NOTIFY_MODE_SMS:
                            smsHelper.send(device.getSn(),linkageFunctionPO.getLinkageName(),user.getPhone());
                            break;
                        default:
                            log.error("unsupported message notify type - {}",notifyModes);
                            break;
                    }
                }
            }
        }catch (Exception e){
            log.error("execute message notify fault",e);
        }
    }

    private void sendApp(LinkageFunctionPO linkageFunctionPO,Device device){
        WsSendMessage message = new WsSendMessage(SendCmdEnum.MESSAGE_NOTIFY);
        message.setBody(new MessageNotifyBody(device.getSn(),linkageFunctionPO.getTitle(),linkageFunctionPO.getContent(),new Date()));
        MessageSendUtil.send(WsChannelUtil.buildAppClientId(device.getOwnerUser()),message);
    }
}
